package cn.edu.uestc.smgt.controller.manager;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.uestc.smgt.common.QueryBase;
import cn.edu.uestc.smgt.pojo.Unit;
import cn.edu.uestc.smgt.service.UnitService;
import cn.edu.uestc.smgt.util.LoginUtil;

@Controller
@RequestMapping("/unit/")
public class UnitController {
	@Autowired
	private UnitService unitService;

	@RequestMapping("list")
	@ResponseBody
	public Object list() {
		return unitService.getAllUnit();
	}

	@RequestMapping("listBySelection")
	@ResponseBody
	public Object listBySelection(HttpServletRequest request, HttpServletResponse response, HttpSession session,
			@RequestParam HashMap map) throws IOException {
		// System.out.println(rows + "," + page);
		QueryBase querybase = new QueryBase();
		querybase.addParameter("id", map.get("id"));
		querybase.addParameter("name", map.get("name"));
		querybase.setPageSize(Long.parseLong(map.get("rows").toString()));
		querybase.setCurrentPage(Long.parseLong(map.get("page").toString()));
		unitService.query(querybase);
		HashMap result = new HashMap();
		result.put("total", querybase.getTotalRow());
		result.put("rows", querybase.getResults());
		return result;
	}

	@RequestMapping("add")
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response, HttpSession session,
			@ModelAttribute Unit unit) throws IOException {
		String message;
		int status = unitService.add(unit);
		if (status == 1) {
			message = "添加成功";
		} else {
			message = "添加失败,查看数据是否重复";
		}
		HashMap result = new HashMap();
		result.put("message", message);
		result.put("status", status);
		return result;
	}

	@RequestMapping("update")
	@ResponseBody
	public Object update(HttpServletRequest request, HttpServletResponse response, HttpSession session,
			@ModelAttribute Unit unit) throws IOException {
		String message;
		int status = unitService.update(unit);
		if (status == 1) {
			message = "修改成功";
		} else {
			message = "修改失败";
		}
		HashMap result = new HashMap();
		result.put("message", message);
		result.put("status", status);
		return result;
	}

	@RequestMapping("delete")
	@ResponseBody
	public Object delete(HttpServletRequest request, HttpServletResponse response, HttpSession session,
			@RequestParam String id) throws IOException {
		String message;
		int status = unitService.delete(id);
		if (status == 1) {
			message = "删除成功";
		} else {
			message = "删除失败";
		}
		HashMap result = new HashMap();
		result.put("message", message);
		result.put("status", status);
		return result;
	}
}
